
package com.micro.boot.base.service;

import java.io.Serializable;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.micro.boot.base.dao.BaseMapper;
import com.micro.boot.common.page.Page;
import com.micro.boot.common.page.PageQuery;

/**
 * @Description: 基类service
 * @param
 * @throws @author
 *             renweiping
 * @e_mail renweiping_1113@google.com
 * @date 2018年9月18日
 */

public abstract class BaseServiceImpl<E, PK extends Serializable> implements BaseService<E, PK> {

	private static final Logger logger = LoggerFactory.getLogger(BaseServiceImpl.class);
	
	
	

	protected abstract BaseMapper<E, PK> getBaseMapper();

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.micro.boot.base.service.BaseService#save(java.lang.Object)
	 */
	@Override
	public int save(E entity) {
		// TODO Auto-generated method stub
		return getBaseMapper().save(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.micro.boot.base.service.BaseService#deleteById(java.lang.Object)
	 */
	@Override
	public int deleteById(PK id) {
		// TODO Auto-generated method stub
		return getBaseMapper().deleteById(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.micro.boot.base.service.BaseService#updateById(java.lang.Object)
	 */
	@Override
	public int updateById(E entity) {
		// TODO Auto-generated method stub
		return getBaseMapper().updateById(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.micro.boot.base.service.BaseService#getById(java.lang.Object)
	 */
	@Override
	public E getById(PK id) {
		// TODO Auto-generated method stub
		return getBaseMapper().getById(id);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.micro.boot.base.service.BaseService#getAll()
	 */
	@Override
	public List<E> getAll() {
		// TODO Auto-generated method stub
		return getBaseMapper().getAll();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.micro.boot.base.service.BaseService#batchInsertEntity(java.util.List)
	 */
	@Override
	public int batchInsertEntity(List<E> entity) {
		// TODO Auto-generated method stub
		return getBaseMapper().batchInsertEntity(entity);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.micro.boot.base.service.BaseService#getByPage(com.micro.boot.common.
	 * page.PageQuery)
	 */
	@Override
	public Page<E> getByPage(PageQuery pageQuery) {

		int cnt = getBaseMapper().getByPageCount(pageQuery);

		if (cnt == 0) {
			return new Page<E>(pageQuery, cnt);
		}

		pageQuery.setEnablePage(true);
		
		List<E> listData = getBaseMapper().getByPage(pageQuery);

		Page<E> page = new Page<E>(pageQuery.getPage(), pageQuery.getPageSize(), cnt, listData);

		return page;
	}

}
